﻿// 取 Grid 中的数据，拼写成 XML 格式
function getGridDataXML(ogrid)
{
	if (ogrid)
	{
		var xmlData = "<dataxml entity=\"p_CwjkHsxmMap\" keyname=\"\">";
		var iRowsLen = ogrid.rows.length;
		var oTr, oTd;
		var sCwztGUID,sHsTypeGUID,sOperObject,sData;
		
		for ( var i = 1; i < iRowsLen; i++)
		{
			oTr = ogrid.rows[i];
			
			if (oTr.entity == "1") 
			{
				sCwztGUID = oTr.cwztguid;
			}
			else
			{
				sCwztGUID = document.all("txtCwztGUID").value;
			}
			
			if (oTr.entity == "1") 
			{
				sHsTypeGUID = oTr.hstypeguid;
			}
			else
			{
				sHsTypeGUID = document.all("txtKmTypeGUID").value;
			}
			
			if (oTr.entity == "1") 
			{
				sOperObject = oTr.operobject;
			}
			else
			{
				sOperObject = document.all("txtOperObject").value;
			}
				
			xmlData += "<row oid=\"" + oTr.oid + "\"" + " objectguid=\"" + oTr.objectguid + "\"" + " buguid=\"" + oTr.buguid
			        + "\"" + " entity=\"" + oTr.entity + "\"" + " cwztguid=\"" + sCwztGUID + "\"" + " hstypeguid=\""
			        + sHsTypeGUID + "\"" + " operobject=\"" + sOperObject + "\">";
			
			// 不取第一列和最后一列的值
			for ( var j = 1; j < ogrid.rows[i].cells.length ; j++)
			{
				oTd = ogrid.rows[i].cells[j];
				
				if (oTd.firstChild.value)
				{
					sData = Trim(oTd.firstChild.value);
				}
				else
				{
					sData = Trim(oTd.innerText);
				}
				
				if(oTd.children.length==3)
				{
					sData=Trim(oTd.children[2].innerText);
				}
				xmlData += "<" + oTd.name + ">" + sData + "</" + oTd.name + ">";
			}

			xmlData += "</row>";
		}

		xmlData += "</dataxml>";
		
		return xmlData;
	}
}

//生成替换
function rptGridData(ogrid,srptdata)
{
	if (ogrid)
	{
		var sNowData = "";
		var iRowsLen = ogrid.rows.length;
		var oTr,oTd,sNowCode,sNowName,sRe;
		var myArray = srptdata.split("!");	
		var myCode,myName,myType;
		if (myArray[0]) myCode = myArray[0].split(":");
		if (myArray[1]) myName = myArray[1].split(":");
		if (myArray[2]) myType = myArray[2].split(":");
		if (myArray[3]) myRule = myArray[3].split(":");
		
		var oldCodeLen=myCode[1].length;
		var oldNameLen=myName[1].length;
		
		for ( var i = 1; i < iRowsLen; i++)
		{
			oTr = ogrid.rows[i];
			// 不取第一列和最后一列的值
			for ( var j = 1; j < ogrid.rows[i].cells.length ; j++)
			{
				oTd = ogrid.rows[i].cells[j];
				
				//保存当前对象代码的原始值
				if (oTd.name == "objectcode") 
				{
					if (oTd.firstChild.value)
					{
						sNowCode = oTd.firstChild.value;
					}
					else
					{
						sNowCode = oTd.innerText;
					}
				}
				
				//保存当前对象名称的原始值
				if (oTd.name == "objectname") 
				{
					if (oTd.firstChild.value)
					{
						sNowName = oTd.firstChild.value;
					}
					else
					{
						sNowName = oTd.innerText;
					}
				}
				
				//开始替换
				
				if (oTd.name == "hsxmcode") 
				{
					if (sNowCode !="" && myCode && myCode.length > 1) 
					{
						if (myRule[1] == "ALL")
						{
							if(oTd.firstChild.value) 
							{
								oTd.firstChild.value = (myCode[2]+sNowCode.substring(oldCodeLen)).replace(/-/g,".");
							}
							else
							{
								oTd.innerText = (myCode[2]+sNowCode.substring(oldCodeLen)).replace(/-/g,".");
							}
						}
						else
						{
							if(oTd.firstChild.value) 
							{
								if(Trim(oTd.firstChild.value) == "") oTd.firstChild.value = (myCode[2]+sNowCode.substring(oldCodeLen)).replace(/-/g,".");
							}
							else
							{
								if(Trim(oTd.innerText) == "") oTd.innerText = (myCode[2]+sNowCode.substring(oldCodeLen)).replace(/-/g,".");
							}
						}
					}
				}
				
				if (oTd.name == "hsxmname" && myName && myName.length > 1) 
				{
					if (sNowName !="") 
					{
						if (myRule[1] == "ALL")
						{
							if(oTd.firstChild.value) 
							{
								oTd.firstChild.value = (myName[2]+sNowName.substring(oldNameLen)).replace(/-/g,"");
							}
							else
							{
								oTd.innerText = (myName[2]+sNowName.substring(oldNameLen)).replace(/-/g,"");
							}
						}
						else
						{
							if(oTd.firstChild.value) 
							{
								if(Trim(oTd.firstChild.value) == "") oTd.firstChild.value = (myName[2]+sNowName.substring(oldNameLen)).replace(/-/g,"");
							}
							else
							{
								if(Trim(oTd.innerText) == "") oTd.innerText = (myName[2]+sNowName.substring(oldNameLen)).replace(/-/g,"");
							}
						}
					}
				}
			}
		}
	}
}

		
//校验款项明细是否存在有效的记录（编辑的情况下）
function verifyGridRow(sXML)
{
	var xmlDom = new ActiveXObject("Microsoft.XMLDOM");
	xmlDom.loadXML(sXML);
	var Nodes = xmlDom.documentElement.selectNodes("//dataxml/row");
	
	if (Nodes.length>0)
	{
		return true;
	}
	
	return false;
}



//导出辅助核算
function ExportHsxmDetail()
{    
    var sGetType = "HSXM";
    var sCwztGUID = document.all("txtCwztGUID").value;
    var sProjGUID = "";
   
    sProjGUID = document.all("txtProjGUID").value;
      
    var sOperObject = document.all("txtOperObject").value;
    var sKmTypeGUID = document.all("txtKmTypeGUID").value;
    var sBz = "";
    var sApplication = document.all("txtApplication").value;
    
    var sPath = "/PubProject/Cwjk/Cwjk_XMLHTTP.aspx?Application="+escape(sApplication);
    sPath+="&GetType="+escape(sGetType);
    sPath+="&CwztGUID="+escape(sCwztGUID);
    sPath+="&ProjGUID="+escape(sProjGUID);
    sPath+="&OperObject="+escape(sOperObject);
    sPath+="&Bz="+escape(sBz);
    sPath+="&KmTypeGUID="+escape(sKmTypeGUID);
    
    strReturn = openXMLHTTP(sPath,"ExportKjkmOrHsxmMap","");
    
    if(strReturn!="")
    {
		//aobo edit by 2010-07-10 将下载的文件名与保存的文件名分开处理，原因为保存的文件名为固定，而存在服务的文件名随机产生
        //var sExportFileName = strReturn.substr(strReturn.lastIndexOf("\\") + 1);
        var sExportFileName = strReturn.substr(strReturn.lastIndexOf("|") + 1);
        var iTempFiles= strReturn.indexOf("\\TempFiles");
        var j= strReturn.lastIndexOf("|");
        var sFilePath = strReturn.substr(iTempFiles,j-iTempFiles);

        OpenDownloadWin(sExportFileName, sFilePath, "", "");
        /*
        var sFile = "Cwjk_DownLoad.aspx?filename="+ escape(strReturn)+ "&random="+Math.random();
        var iTop,iLeft;
        iTop = (screen.height - 200)/2;
        iLeft = (screen.width - 400)/2;
		window.open(sFile, "", "width=1px,height=1px,left="+ iLeft +"px,top="+ iTop +"px,status=0,resizable=1,menubar=yes");
		*/
    }
}
	
//导入辅助核算对照信息
function ImportHsxmDetail()
{
    var sGetType = "HSXM";
    var sCwztGUID = document.all("txtCwztGUID").value;
    var sOperObject = document.all("txtOperObject").value;
    var sKmTypeGUID = document.all("txtKmTypeGUID").value;
    var sProjGUID = document.all("txtProjGUID").value;
    
    var sTitle, sHeight, sFile, sParams;
    var iWidth, iHeight;

    sTitle = "选择文件";
    sFile = "/PubProject/Cwjk/Cwjk_Hsxm_Import.aspx";
    sWidth = "680";
    sHeight = "480";
    
    // 后缀参数
    var sParams = "";
    sParams+= "GetType=" + escape(sGetType);
    sParams+= "&CwztGUID=" + escape(sCwztGUID);
    sParams+= "&OperObject=" + escape(sOperObject);
    sParams+= "&KmTypeGUID=" + escape(sKmTypeGUID);
    sParams+= "&ProjGUID=" + escape(sProjGUID);

    var url = "/FrameTemp0.aspx";
    url += "?title=" + escape(sTitle);
    url += "&filename=" + escape(sFile);
    url += "&param=" + escape(sParams);
	
    var sReturnValue = window.showModalDialog(url, "", "dialogWidth:" + sWidth + "px; dialogHeight:" + sHeight + "px; status:no; help:no; resizable:no;scroll:no;");
		
    if(sReturnValue=="OK")
    {
       alert("辅助核算导入成功！");
       __doPostBack("","");
    }
    else
    {
        
    }
}	

//生成
function batchSet()
{
    if(!confirm("确实要批量生成核算代码和核算名称吗？")) 
    {
        return;
    }
    
    var ogrid=gridBar;
    
    if (ogrid)
	{
		var sNowData = "";
		var iRowsLen = ogrid.rows.length;
		var oTr,oTd,sNowCode,sNowName,sRe;
		
		for ( var i = 1; i < iRowsLen; i++)
		{
			oTr = ogrid.rows[i];
			// 不取第一列
			for ( var j = 1; j < ogrid.rows[i].cells.length ; j++)
			{
				oTd = ogrid.rows[i].cells[j];
				
				//保存当前对象代码的原始值
				if (oTd.name == "objectcode") 
				{
					if (oTd.firstChild.value)
					{
						sNowCode = oTd.firstChild.value;
					}
					else
					{
						sNowCode = oTd.innerText;
					}
				}
				
				//保存当前对象名称的原始值
				if (oTd.name == "objectname") 
				{
					if (oTd.firstChild.value)
					{
						sNowName = Trim(oTd.firstChild.value.replace("[-]","").replace("[+]",""));
					}
					else
					{
						sNowName = Trim(oTd.innerText.replace("[-]","").replace("[+]",""));
					}
				}
				
				//开始替换
				if (oTd.name == "hsxmcode") 
				{
					if(oTd.firstChild.value) 
					{
						oTd.firstChild.value = sNowCode;
					}
					else
					{
						oTd.innerText = sNowCode;
					}	
				}
				
				if (oTd.name == "hsxmname") 
				{
					if(oTd.firstChild.value) 
					{
						oTd.firstChild.value = sNowName;
					}
					else
					{
						oTd.innerText = sNowName;
					}	
				}
		    }
	    } 
    
    }

}



//批量录入
function batchInput()
{
    var sMyarray,sFile,sParams;	
    
    if(document.all("txtOperObject").value=="")
    {
       alert("请选择辅助核算！")
       return;	
    }
    
    var bCheckHasSelectItem = false;
    
    if (gridBar)
	{
	    var oTd ;
	    for ( var i = 1; i < gridBar.rows.length; i++)
	    {
	        oTd = gridBar.rows[i].cells[0];
	        if (oTd.name == "objectItemCheckBox") 
		    {
		        if (oTd.firstChild.checked)
		        {
		            bCheckHasSelectItem = oTd.firstChild.checked;
		            break;
		        } 
		    }
        }
	}
    
    
    if (!bCheckHasSelectItem)
    {
        alert("请选择批量录入的记录！");
        return;
    }
    
	sParams = "&openroot=" + escape(document.all("txtOpenRoot").value);
	sParams += "&operobject=" + escape(document.all("txtOperObject").value);

	var sTitle,sHeight,sWidth,sFile,sReturn;
	
	sTitle = "批量录入";
	sHeight = "160";
	sWidth = "350";
	sFile = "/PubProject/CWJK/Cwjk_BatchInput.aspx";		
					
	sReturn = openMyDlg(sTitle,sFile,sParams,"",sWidth,sHeight);

	if ( sReturn != undefined && sReturn != "")
	{	    
	    var myCode,myName;
	    var arrTemp = sReturn.split("!");
		if (arrTemp[0]) myCode = arrTemp[0];
		if (arrTemp[1]) myName = arrTemp[1];

		//页面值替换
		if (gridBar)
		{
			var iRowsLen = gridBar.rows.length;
		    var oTr,oTd ,checkResult;
		    for ( var i = 1; i < iRowsLen; i++)
		    {
		        checkResult = false;
		        oTr = gridBar.rows[i];		        
		        // 不取第一列
			    for ( var j = 0; j < gridBar.rows[i].cells.length ; j++)
			    {
				    oTd = gridBar.rows[i].cells[j];				    
				    //校验是否勾选
				    if (oTd.name == "objectItemCheckBox") 
		            {
		                checkResult = oTd.firstChild.checked;
				    }
				    if (checkResult)
	                {
			            //开始替换
			            if (oTd.name == "hsxmcode") 
			            {
			                if(oTd.firstChild.value) 
			                {
				                oTd.firstChild.value = myCode;
			                }
			                else
			                {
				                oTd.innerText = myCode;
			                }
			            }
        				
			            if (oTd.name == "hsxmname") 
			            {
			                if(oTd.firstChild.value) 
			                {
				                oTd.firstChild.value = myName;
			                }
			                else
			                {
				                oTd.innerText = myName;
			                }
			            }
			        }
				}
		    }
		}
	}

}

function BeForeExportHsxmDetail()
{
   document.all.txtIsExport.value="Export";
   saveGridData();
}

function copyToOtherZT()
{
    var sBUGUID = document.all("txtBUGUID").value;
    var sCwztGUID = document.all("txtCwztGUID").value;
    var sApplication = document.all("txtApplication").value;    
    
    var sPath = "/PubProject/Cwjk/Cwjk_XMLHTTP.aspx?Application="+escape(sApplication)+"&BUGUID="+escape(sBUGUID)+"&CwztGUID="+escape(sCwztGUID);
    var strReturn = openXMLHTTP(sPath,"GetNeedToCopyCwztCount","");
    
    if (strReturn=="0")
    {
        alert("当前公司下没有需要同步会计辅助核算的账套！");
        return;
    }
    
    if (confirm("确实将供应商辅助核算明细同步到其它账套吗？"))
    {
        strReturn = openXMLHTTP(sPath,"CopyCwjkHsxmMap","");
        
        if (strReturn=="0" || strReturn=="99")
        {
            alert("会计辅助核算同步失败！");
            return;
        }
        else
        {
            alert("成功同步了"+ strReturn +"个账套！");
            return;
        }
    }
}

function SelectAllObject()
{
    if (gridBar)
    {
        var oTd ;
	    for ( var i = 1; i < gridBar.rows.length; i++)
	    {
	        oTd = gridBar.rows[i].cells[0];
	        if (oTd.name == "objectItemCheckBox") 
		    {
		        oTd.firstChild.checked = document.getElementById("objectHeaderCheckBox").checked;
		    }
        }
    }
    return;
}
